#include "dataviewer.h"
#include "qwt_plot_curve.h"
#include "datacurve.h"
#include <QDebug>
#include <qwt_plot_directpainter.h>
#include <QFile>
#include <QFileDialog>
#include "myevent.h"
DataViewer::DataViewer(QString& name,int num_plot_, QWidget *parent)
: QWidget(parent), UiOfDataViewer(this, num_plot_), num_plot(num_plot_), 
plot_pause(false),icon_play(),icon_pause(),icon_hide(),icon_show()
{
	icon_play.addFile(":/images/play.png");
	icon_pause.addFile(":/images/pause.png");
	icon_hide.addFile(":/images/hide.png");
	icon_show.addFile(":/images/show.png");
	bn_pause->setIcon(icon_pause);
	bn_pause->setIconSize(QSize(32, 32));
	bn_hide->setIcon(icon_hide);
	bn_hide->setIconSize(QSize(8, 8));

	setObjectName(name);

	QwtText xtitle(QApplication::tr("Time(s)"));
	QFont font("\345\256\213\344\275\223", 9);
	plot[num_plot - 1]->setAxisTitle(QwtPlot::xBottom, xtitle);
	xtitle.setFont(font);

	for (int i = 0; i < num_plot;i++)
		connect(plot[i], SIGNAL(maximize(Plot*)), this, SLOT(maximizePlot(Plot*)));
	connect(bn_pause, SIGNAL(clicked()), this, SLOT(pause()));
	connect(bn_auto, SIGNAL(clicked()), this, SLOT(autoScale()));
	connect(bn_hide, SIGNAL(clicked()), this, SLOT(hideRightBar()));
	connect(rb_wheel_zoom, SIGNAL(clicked()), this, SLOT(setWheelZoomer()));
	connect(rb_key_zoom, SIGNAL(clicked()), this, SLOT(setKeyZoomer()));
	connect(bn_auto, SIGNAL(clicked()), this, SLOT(autoScale()));
	connect(wb_xrange, SIGNAL(valueChanged(double)), this, SLOT(updateXAxisWidth(double)));
	connect(label_save, SIGNAL(linkActivated(const QString&)), this, SLOT(saveData(const QString&)));
}
bool DataViewer::event(QEvent* event_)
{
	DataEvent* data_event = static_cast<DataEvent*>(event_);
	if (data_event->type() == EVENT_TYPE_DATA)
	{
		lcd_feq->display(data_event->feq());
		for (int i = 0; i < num_plot; i++)
		{
			if (!plot_pause)
			{
				lcd_vlaue[i]->display(data_event->dataAt(i));
				plot[i]->plot(data_event->timeStamp(),
					data_event->dataAt(i),
					data_event->feq());
			}
		}
	}
	return QWidget::event(event_);
}
void DataViewer::pause()
{
	plot_pause = !plot_pause;
	fra_zoom->setEnabled(!fra_zoom->isEnabled());
	if (plot_pause)
	{
		bn_pause->setIcon(icon_play);
		bn_pause->setIconSize(QSize(25, 25));
		if (rb_key_zoom->isChecked())
			setKeyZoomer();
		else
			setWheelZoomer();
	}
	else
	{
		bn_pause->setIcon(icon_pause);
		bn_pause->setIconSize(QSize(25, 25));
		for (int i = 0; i < num_plot; i++)
			plot[i]->initZoomer();
		autoScale();
	}
}
DataViewer::~DataViewer()
{

}

void DataViewer::updateXAxisWidth(double w)
{
	for (int i = 0; i < num_plot; i++)
		plot[i]->updateXAxisWidth(w);
}
void DataViewer::autoScale()
{
	for (int i = 0; i < num_plot; i++)
		plot[i]->autoScale();
}

void DataViewer::setKeyZoomer()
{
	for (int i = 0; i < num_plot; i++)
		plot[i]->setKeyZoomer();
}
void DataViewer::setWheelZoomer()
{
	for (int i = 0; i < num_plot; i++)
		plot[i]->setWheelZoomer();
}
void DataViewer::maximizePlot(Plot* p)
{
	for (int i = 0; i < num_plot; i++)
	{
		if (p != plot[i])
			plot[i]->setVisible(!plot[i]->isVisible());
		plot[i]->replot();
	}
}
void DataViewer::setYAxisTitle(QString& y_title)
{
	QwtText y_text;
	for (int i = 0; i < num_plot;i++)
	{
		y_text.setText(y_title);
		plot[i]->setAxisTitle(QwtPlot::yLeft, y_text);
	}
}
void DataViewer::setValueTitle(QStringList& v_titles)
{
	for (int i = 0; i < num_plot; i++)
		label_vlaue[i]->setText(v_titles[i]);
}
void DataViewer::setPlotTitle(QStringList& p_titles)
{
	QwtText title;
	QFont font("\345\256\213\344\275\223", 9);
	title.setFont(font);

	for (int i = 0; i < num_plot; i++)
	{
		title.setText(p_titles[i]);
		plot[i]->setTitle(title);
	}
		
}
void DataViewer::hideRightBar()
{
	fra_para->setVisible(!fra_para->isVisible());
	if (fra_para->isVisible())
		bn_hide->setIcon(icon_hide);
	else
		bn_hide->setIcon(icon_show);
}
void DataViewer::saveData(const QString&)
{
	QString fileName = 
		QFileDialog::getSaveFileName(this,
		QApplication::tr("Select File"),
		windowTitle()+QString(".txt"),
		QApplication::tr("Text File(*.txt)"));

	if (!fileName.isNull())
	{
		QFile f(fileName);
		f.open(QIODevice::Text | QIODevice::ReadWrite);
		QTextStream file(&f);
		file << QApplication::tr("Time");
		for (int i = 0; i < num_plot; i++)
			file << " " << plot[i]->title().text();
		file << "\n";
		int data_len = plot[0]->dataLength();
		for (int i = 0; i < data_len; i++)
		{
			file << plot[0]->x(i);
			for (int j = 0; j < num_plot; j++)
				file << " " << plot[j]->y(i);
			file << "\n";
		}
		f.close();
	}
}
void DataViewer::closeEvent(QCloseEvent* event_)
{
	emit winClose(objectName());
	QWidget::closeEvent(event_);
}